//
//  CECaptialSettingCell.m
//  digitalCurrency
//
//  Created by 默认账户 on 2019/4/3.
//  Copyright © 2019 XinHuoKeJi. All rights reserved.
//

#import "CECaptialSettingVC.h"
#import "CESettingsCell.h"
#import "MineNetManager.h"
#import "MSVerifyCell.h"
#import "CustomButton.h"

@interface CECaptialSettingVC ()

@property (nonatomic, copy) NSArray *titles;
@property (nonatomic, strong) UIButton *besureBtn;
@property (nonatomic, strong) UITextField *moneyPassword;
@property (nonatomic, strong) UITextField *certainPassword;
@property (nonatomic, strong) UITextField *verifyCode;

@end

@implementation CECaptialSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.viewTitle.text = LocalizationKey(@"Set asset password");
    
    [self.view addSubview:self.tableView];
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 200);
    self.tableView.rowHeight = 65;
    self.tableView.separatorColor = [UIColor whiteColor];

    [self.tableView registerClass:[CESettingsCell class] forCellReuseIdentifier:@"setcell"];
    
    _titles = @[LocalizationKey(@"Please enter new password"),LocalizationKey(@"Please enter new password again")];
    
    [self.view addSubview:self.besureBtn];
//    [ToolUtil view:self.besureBtn gradineViewOne:self.besureBtn.bounds colorOne:[UIColor colorWithHEX:maincolor] colorTwo:[UIColor colorWithHEX:mainGradientColor]];

    @weakify(self);
    [[self.besureBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self submit];
    }];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,40,0,40)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,40,0,40)];
    }
}
    
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CESettingsCell *cell = [CESettingsCell createTableView:tableView];
    cell.placeHolder = _titles[indexPath.row];
    switch (indexPath.row) {
        case 0:
            self.moneyPassword = cell.textField;
            self.moneyPassword.keyboardType = UIKeyboardTypeNumberPad;
            break;
        case 1:
            self.certainPassword = cell.textField;
            self.certainPassword.keyboardType = UIKeyboardTypeNumberPad;
            break;
        default:
            break;
    }
    return cell;
    
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0,40,0,40)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0,40,0,40)];
    }
}
    
//MARK:--设置资金密码的接口
-(void)submit {
    NSString *moneyPassword   = self.moneyPassword.text;
    NSString *certainPassword = self.certainPassword.text;
    
    if ([moneyPassword isEqualToString:@""]) {
        [self.view makeToast:LocalizationKey(@"inputNewMoneyPassword") duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([certainPassword isEqualToString:@""]) {
        [self.view makeToast:LocalizationKey(@"inputCertainMoneyPassword") duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (![certainPassword isEqualToString:moneyPassword]) {
        [self.view makeToast:LocalizationKey(@"checkPassword") duration:1.5 position:CSToastPositionCenter];
        return;
    }
//    if (![ToolUtil matchPassword:moneyPassword]) {
//        [self.view makeToast:LocalizationKey(@"Enter a password combining 8-20 digits and letters") duration:1.5 position:CSToastPositionCenter];
//        return;
//    }
    NSString *regex = @"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    if (![pred evaluateWithObject:moneyPassword] || moneyPassword.length != 6) {
        [self.view makeToast:LocalizationKey(@"请输入六位长度的密码") duration:1.5 position:CSToastPositionCenter];
               return;
    }
    
    [MineNetManager moneyPasswordForJyPassword:moneyPassword CompleteHandle:^(id resPonseObj, int code) {
        [EasyShowLodingView hidenLoding];
        NSInteger codeint = [resPonseObj[@"code"] integerValue];
        if (code) {
            if (codeint == 0) {
                //获取数据成功
                [self.view makeToast:resPonseObj[MESSAGE] duration:1.0 position:CSToastPositionCenter];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
            else if (codeint == -1000001 ){
                [ShowLoGinVC showLoginVc:self withTipMessage:resPonseObj[MESSAGE]];
            }
            else {
                [self.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
            }
        }
        else {
            [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
        }
    }];
}

- (UIButton *)besureBtn {
    if (!_besureBtn) {
        _besureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _besureBtn.layer.masksToBounds = YES;
        _besureBtn.layer.cornerRadius = 22.0;
        [_besureBtn setTitle:LocalizationKey(@"Confirm") forState:UIControlStateNormal];
        [_besureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _besureBtn.backgroundColor = [UIColor colorWithHEX:maincolor];
        _besureBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        _besureBtn.frame = CGRectMake(23, 200, SCREEN_WIDTH-46, 44);
    }
    
    return _besureBtn;
}

@end
